package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址管理相关接口")
public class AddressController {

    @Resource
    private AddressBookService addressBookService;

    /*
     * 查看地址列表
     * */
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        log.info("查看地址列表");
        List<AddressBook> addressBooks = addressBookService.list();
        return Result.success(addressBooks);
    }

    /*
     * 根据id查询地址
     * */
    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id) {
        log.info("根据id查询地址:{}", id);
        AddressBook addressBook = addressBookService.queryById(id);
        return Result.success(addressBook);
    }

    /*
     * 修改地址
     * */
    @ApiOperation("修改地址")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook) {
        log.info("修改地址:{}", addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }

    /*
     * 根据id删除地址
     * */
    @DeleteMapping
    public Result delete(Long id) {
        log.info("删除地址id:{}", id);
        addressBookService.delete(id);
        return Result.success();
    }

    /*
     * 设为默认地址
     * */
    @ApiOperation("设为默认地址")
    @PutMapping("/default")
    public Result isDefault(@RequestBody AddressBook addressBook) {
        log.info("设为默认地址:{}", addressBook);
        addressBookService.setIsDefault(addressBook);
        return Result.success();
    }

    /*
    * 新增地址
    * */
    @ApiOperation("新增地址")
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址:{}",addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }

    /*
    * 查询默认地址
    * */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result getDefault(){
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.queryDefault();
        return Result.success(addressBook);
    }
}
